using System;
using System.Collections.Generic;
using System.Text;

namespace Developex.DatabaseWrapper.Fields
{
	/// <summary>
	/// 
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class ForeignField<T> : Field<T>
	{
		#region Fields

		private string foreignTableName;
		private string foreignFieldName;

		#endregion

		#region Constructors

		/// <summary>
		/// 
		/// </summary>
		/// <param name="foreignTableName"></param>
		/// <param name="foreignFieldName"></param>
		/// <param name="name"></param>
		public ForeignField(string foreignTableName, string foreignFieldName, string name)
			: base(name, FieldFlags.Fictitious)
		{
			this.foreignTableName = foreignTableName;
			this.foreignFieldName = foreignFieldName;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="foreignTableName"></param>
		/// <param name="foreignFieldName"></param>
		/// <param name="name"></param>
		/// <param name="flags"></param>
		public ForeignField(string foreignTableName, string foreignFieldName, string name, FieldFlags flags)
			: base(name, flags | FieldFlags.Fictitious)
		{
			this.foreignTableName = foreignTableName;
			this.foreignFieldName = foreignFieldName;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="foreignTableName"></param>
		/// <param name="foreignFieldName"></param>
		/// <param name="name"></param>
		/// <param name="flags"></param>
		/// <param name="insertDefaultValue"></param>
		public ForeignField(string foreignTableName, string foreignFieldName, string name, FieldFlags flags, T insertDefaultValue)
			: base(name, flags | FieldFlags.Fictitious, insertDefaultValue)
		{
			this.foreignTableName = foreignTableName;
			this.foreignFieldName = foreignFieldName;
		}

		#endregion

		#region Overrides

		/// <summary>
		/// 
		/// </summary>
		public override string GetSelectExpression()
		{
			return string.Format("[{0}].[{1}]", foreignTableName, foreignFieldName);
		}

		/// <summary>
		/// Clone
		/// </summary>
		/// <returns></returns>
		public override object Clone()
		{
			ForeignField<T> field = (ForeignField<T>)base.Clone();
			field.foreignFieldName = foreignFieldName;
			field.foreignTableName = foreignTableName;
			return field;
		}

		///<summary>
		/// Create object 
		///</summary>
		///<returns></returns>
		/// <remarks>
		/// Here should be only object creation, all data copy should be in overrided Clone function.
		/// Used only in base version of Clone function.
		/// Should be overrided for ICloneable interface.
		/// </remarks>
		protected override Field<T> CreateObjectForClone()
		{
			return new ForeignField<T>(null, null, null);
		}

		#endregion
	}
}
